home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / math / gle-3.000 / gle-3 / gle / general.c < prev    next >
C/C++ Source or Header  |  1995-02-07  |  2KB  |  107 lines

  1. #define halfpi 1.57079632679489661923
  2. #define pi 3.14159265358979323846
  3. #include <math.h>
  4. #include "all.h"
  5.  
  6. #ifdef VAXC
  7. double myatan2(double y, double x);
  8. #endif
  9.  
  10. showpcode(int32 *p)
  11. {
  12.     union {int32 l; short s[2];} bth;
  13.     int i;
  14.  
  15.     gprint("GP> ");
  16.     for (i=0;i<4;i++) {
  17.         bth.l = *(p++);
  18.         gprint("%x %x  ",bth.s[0],bth.s[1]);
  19.     }
  20.     gprint("\n");
  21. }
  22. polar_xy(double r, double angle, double *dx, double *dy)
  23. {
  24.     *dx = r*cos(angle*3.14159265/180);
  25.     *dy = r*sin(angle*3.14159265/180);
  26. }
  27. xy_polar(double dx,double dy,double *radius,double *angle)
  28. {
  29.     if (dx==0 && dy==0) {
  30.         gprint("Cannot work out angle of zero length vector\n");
  31.         return;
  32.     }
  33.     if (dx==0) {
  34.         *angle = 90;
  35.         if (dy<0) *angle = -90;
  36.     } else {
  37.         *angle = myatan2(dy,dx)*180/pi;
  38.     }
  39.     *radius = sqrt(dx*dx + dy*dy);
  40. }
  41.  
  42. fpolar_xy(float r, float angle, float *dx, float *dy)
  43. {
  44.     *dx = r*cos(angle*3.14159265/180);
  45.     *dy = r*sin(angle*3.14159265/180);
  46. }
  47. fxy_polar(float dx,float dy,float *radius,float *angle)
  48. {
  49.     if (dx==0 && dy==0) {
  50.         gprint("Cannot work out angle of zero length vector\n");
  51.         return;
  52.     }
  53.     if (dx==0) {
  54.         *angle = 90;
  55.         if (dy<0) *angle = -90;
  56.     } else {
  57.         *angle = myatan2(dy,dx)*180/pi;
  58.     }
  59.     *radius = sqrt(dx*dx+dy*dy);
  60. }
  61.  
  62. ncpy(char *d, char *s, int n)
  63. {
  64.     strncpy(d,s,n);
  65.     *(d+n) = 0;
  66. }
  67. ncat(char *d, char *s, int n)
  68. {
  69.     int i;
  70.     i = strlen(d);
  71.     strncat(d,s,n);
  72.     *(d+i+n) = 0;
  73. }
  74. #ifdef VAXC
  75. double myatan2(double y, double x)
  76. {
  77.     static double one,test,xx,yy,zero,at2;
  78.     zero = 0;
  79.     one = 1;
  80.     xx = fabs(x);
  81.     yy = fabs(y);
  82.     if (x==0) {
  83.         at2 = halfpi;
  84.     } else {
  85.         if (yy<=xx) {
  86.             at2 = fabs(atan(yy/xx));
  87.         } else {
  88.             test = one + (xx/yy);
  89.             if (test!=one) {
  90.                 at2 = fabs(atan(yy/xx));
  91.             } else {
  92.                 at2 = halfpi;
  93.             }
  94.         }
  95.         if (x<zero) at2 = pi - at2;
  96.     }
  97.     if (y<0) at2 = -at2;
  98.     return at2;
  99. }
  100. #else
  101. double myatan2(double y, double x)
  102. {
  103.     return atan2(y,x);
  104. }
  105. #endif
  106.  
  107.